Java8 Stream的终止操作使用示例

package com.expgiga.Java8;

/**
 *
 */
public class Employee {
    private String name;
    private int age;
    private double salary;
    private int id;

    private Status status;

    public Employee() {
    }

    public Employee(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public Employee(String name, int age, double salary, Status status) {
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.status = status;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                ", id=" + id +
                ", status=" + status +
                '}';
    }

    public Employee(int id) {
        this.id = id;
    }

    public Employee(int id, int age) {
        this.id = id;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Employee employee = (Employee) o;

        if (age != employee.age) return false;
        if (Double.compare(employee.salary, salary) != 0) return false;
        if (id != employee.id) return false;
        return name != null ? name.equals(employee.name) : employee.name == null;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        temp = Double.doubleToLongBits(salary);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + id;
        return result;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public enum Status {
        FREE,
        BUSY,
        VOCATION;
    }
}

package com.expgiga.Java8;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * Java8 Stream的终止操作:
 *
 */
public class TestStreamAPI {
    public static void main(String[] args) {
        /*
         * 查找与匹配
          * allMatch
          * anyMatch
          * noneMatch
          * findFirst
          * findAny
          * count
          * max
          * min
          *
         */
        List<Employee> employeeList = Arrays.asList(
                new Employee("zhangsan", 18, 19999, Employee.Status.FREE),
                new Employee("lisi", 28, 29999, Employee.Status.BUSY),
                new Employee("wangwu", 38, 39999, Employee.Status.VOCATION),
                new Employee("zhaoliu", 16, 17999, Employee.Status.BUSY),
                new Employee("tianqi", 6, 12999, Employee.Status.FREE)
        );

        boolean b1 = employeeList.stream()
                .allMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b1); //false

        boolean b2 = employeeList.stream()
                .anyMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b2);

        boolean b3 = employeeList.stream()
                .noneMatch((e) -> e.getStatus().equals(Employee.Status.FREE));
        System.out.println(b3);

        Optional<Employee> op = employeeList.stream()
                .sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()))
                .findFirst();
        System.out.println(op.get());

        Optional<Employee> op2 = employeeList.parallelStream()
                .filter((e) -> e.getStatus().equals(Employee.Status.FREE))
                .findAny();
        System.out.println(op2.get());

        Long count = employeeList.stream()
                .count();
        System.out.println(count);

        Optional<Employee> op3 = employeeList.stream()
                .max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(op3.get());

        Optional<Double> op4 = employeeList.stream()
                .map((Employee::getSalary))
                .min(Double::compare);
        System.out.println(op4.get());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值